package net.code.btalk.ui;

import com.hoodemia.ui.SuperDuperFieldManager;

import net.code.btalk.BTalk;
import net.code.btalk.BTalkResource;
import net.code.btalk.Buddy;
import net.code.btalk.Notification;
import net.code.btalk.SavedData;
import net.rim.blackberry.api.messagelist.ApplicationIndicator;
import net.rim.blackberry.api.messagelist.ApplicationIndicatorRegistry;
import net.rim.device.api.system.DeviceInfo;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.util.StringProvider;

public class MessageScreen extends MainScreen {
	private final Buddy buddy;
//	private int messageCounter = 0;
//	private static int FONT_TITLE = 0;
//	private static int FONT_BODY = 1;
	
	private VerticalFieldManager vm1;
//	private SuperDuperFieldManager vm2;
	private VerticalFieldManager vm2;
	
	private MessageTextField msgTextField = new MessageTextField();
	
//	private static final Font[] fonts = new Font[] {Font.getDefault().derive(Font.BOLD|Font.UNDERLINED, SavedData.fontSize),
//		Font.getDefault().derive(Font.PLAIN, SavedData.fontSize)};
	
//	private static final byte[] textAttr = new byte[] {0, 1};
//	
//	private static int[] offsets_me = new int[] {0, "me:".length(), 0};
	private int[] offsets_buddy = new int[3];
	
	public MessageScreen(Buddy b) {
		super(MainScreen.NO_VERTICAL_SCROLL & MainScreen.NO_VERTICAL_SCROLLBAR);

		// cuma pake text field saja, tidak ada button. jika ingin mengirim pesan tekan enter pada keypad bb.
		msgTextField.setPadding(6, 2, 7, 5);
		msgTextField.setBackground(Constants.BACKGROUND_WHITE_PLAIN);
		msgTextField.setBorder(ButtonField.VISUAL_STATE_NORMAL, Constants.SIMPLE_GRAY_BORDER, true);
		msgTextField.setBorder(ButtonField.VISUAL_STATE_FOCUS, Constants.SIMPLE_ORANGE_BORDER, true);
		msgTextField.setBorder(ButtonField.VISUAL_STATE_ACTIVE, Constants.SIMPLE_ORANGE_BORDER, true);
		
		buddy = b;
		this.setTitle("Chat with " + b.name);
		offsets_buddy[0] = 0;
		offsets_buddy[1] = b.name.length()+1;
		vm1 = new VerticalFieldManager();
		vm2 = new VerticalFieldManager(Field.FIELD_HCENTER);
		int spaces = Display.getWidth() / 20;
		vm2.setPadding(0, spaces, 0, spaces);
		vm2.setBackground(Constants.BACKGROUND_MODERATEGREY_GRADIENT);
		
		this.add(vm1);
		this.setStatus(vm2);

		this.addMenuItem(new MenuItem(new StringProvider(BTalkLocale.getString(BTalkResource.CHAT_SCREEN_MI_SEND)), 0, 0x0) {
			public void run() {
				msgTextField.keyChar((char) Keypad.KEY_ENTER, 0, 0);			
			}
		});

		this.addMenuItem(new MenuItem(new StringProvider(BTalkLocale.getString(BTalkResource.CHAT_SCREEN_MI_CLS)), 0, 0x1000000) {
			public void run() {
				vm1.deleteAll();
				vm1.add(new RichTextField("\n\n\n\n\n\n\n\n\n\n\n\n\n"));
			}
		});
		
		//FIXME need a better way to locate editfield at the bottom
		vm1.add(new RichTextField("\n\n\n\n\n\n\n\n\n\n\n\n\n"));
	}
	
	// needn't check whether this is the current buddy
	public void sendMessage(String msg, boolean showTitle, String time) {
		if (showTitle) {
			SuperDuperFieldManager sdfm = new SuperDuperFieldManager(SuperDuperFieldManager.JUSTIFY_LEFT_AND_RIGHT);
			sdfm.setBackground(Constants.BACKGROUND_LIGHTGREY_PLAIN);

			LabelField t1 = new LabelField("me", Field.FIELD_LEFT);
			LabelField t3 = new LabelField(time, Field.FIELD_RIGHT);

			t1.setFont(Constants.FONT_CHAT_BOLD_STYLE);
			t3.setFont(Constants.FONT_CHAT_PLAIN_STYLE);
			
			sdfm.add(t1);
			sdfm.add(t3);

			vm1.add(sdfm);
		}
		
		RichTextField t2 = new RichTextField(msg);
		t2.setFont(Constants.FONT_CHAT_PLAIN_STYLE);
		this.vm1.add(t2);
		t2.setPadding(0, 10, 0, 10);
		t2.setFocus();

		msgTextField.setFocus();
	}
	
	// when pushed into stack
	protected void onDisplay() {
		this.vm2.add(msgTextField);
		msgTextField.setFocus();
		
		// FIXME app need to be signed to make this function could run on device
		/*if (DeviceInfo.isSimulator()) {
			try {
				ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry
						.getInstance();
				ApplicationIndicator indicator = reg.getApplicationIndicator();

				Notification.totalUnreadMessages -= buddy.unreadMessages;
				indicator.setValue(Notification.totalUnreadMessages);
				if (Notification.totalUnreadMessages == 0) {
					indicator.setVisible(false);
				}
				buddy.unreadMessages = 0;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}*/
	}
	
	// when popped off the stack
	protected void onUndisplay() {
		this.vm2.delete(msgTextField);
		int idx = Buddy.btalk.buddyList.getBuddyIndex(buddy);
		idx = (idx > 0 ) ? idx : 0;
		Buddy.btalk.buddyList.setSelectedIndex(Buddy.btalk.buddyList.getBuddyIndex(buddy));
		Buddy.btalk.currentBuddy = null;
	}
	
//	public void increaseCounter() {
//		messageCounter++;
//	}
	
	public void receiveMessage(String msg, boolean current, boolean showTitle, String time) {
		if (showTitle) {
			SuperDuperFieldManager sdfm = new SuperDuperFieldManager(SuperDuperFieldManager.JUSTIFY_LEFT_AND_RIGHT);
			sdfm.setBackground(Constants.BACKGROUND_LIGHTGREY_PLAIN);

			LabelField t1 = new LabelField(buddy.name, Field.FIELD_LEFT);
			LabelField t3 = new LabelField(time, Field.FIELD_RIGHT);

			t1.setFont(Constants.FONT_CHAT_BOLD_STYLE);
			t3.setFont(Constants.FONT_CHAT_PLAIN_STYLE);
			
			sdfm.add(t1);
			sdfm.add(t3);

			vm1.add(sdfm);
		}
		
		RichTextField t2 = new RichTextField(msg);
		t2.setFont(Constants.FONT_CHAT_PLAIN_STYLE);
		t2.setPadding(0, 10, 0, 10);
		this.vm1.add(t2);
		
		if (current) {
			t2.setFocus();
			msgTextField.setFocus();
		}
	}
	
	// handle the trackball/trackwheel click event
	public boolean onMenu(int instance) {
		if (SavedData.sendWithMenu) {
			msgTextField.keyChar((char) Keypad.KEY_ENTER, 0, 0);
			return true;
		} else {
			return super.onMenu(instance);
		}
	}
	
	protected boolean keyChar(char key, int status, int time) {
		if (key == Keypad.KEY_ENTER) {
			if (this.getLeafFieldWithFocus() != msgTextField) {
				msgTextField.setFocus();
				return true;
			}
		}
		return super.keyChar(key, status, time);
	}
	
	public boolean onClose() {
		if (this.getLeafFieldWithFocus() == msgTextField && msgTextField.getText().length() > 0) {
			msgTextField.setText("");
			return false;
		} else {
			msgTextField.setText("");
			this.close();
			return true;
		}
	}
	
	/*public void initFields(MessageListField msgListField, TextField text) {
		this.add(msgListField);
		this.add(new SeparatorField());
		this.add(text);
		text.setFocus();
	}*/
	
}
